package com.example.understand_compose1.testcase_compose.page

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.rememberImagePainter
import kotlinx.coroutines.launch

// 不可滑动
@Preview
@Composable
fun SimpleColumn() {
    Column {
        repeat(100) {
            Text(
                text = "Item #$it",
                style = MaterialTheme.typography.titleSmall
            )
        }
    }
}

// 可滑动的Item
@Preview
@Composable
fun SimpleList() {
    val scrollState = rememberScrollState()
    Column(modifier = Modifier.verticalScroll(scrollState)) {
        repeat(100) {
            Text(
                text = "Item #$it",
                style = MaterialTheme.typography.titleMedium
            )
        }
    }
}

@Preview
@Composable
fun LazyList() {
    val scrollState = rememberLazyListState()
    LazyColumn(state = scrollState) {
        items(100) {
            Text(text = "Item #$it", style = MaterialTheme.typography.titleLarge)
        }
    }
}

@Preview
@Composable
fun ScrollingList() {
    val listSize = 100

    val scrollState = rememberLazyListState()

    val coroutineScope = rememberCoroutineScope()
    Column {
        Row {
            Button(onClick = {
                // BUG:
                coroutineScope.launch {
                scrollState.animateScrollToItem(0)
                }
            }, modifier = Modifier.weight(1f)) {
                Text(text = "滑到头部")
            }
            Button(onClick = {
                // BUG:
                coroutineScope.launch {
                    scrollState.animateScrollToItem(Int.MAX_VALUE)
                }
            }, modifier = Modifier.weight(1f)) {
                Text(text = "滑到底部")
            }
        }

        val scrollState = rememberLazyListState()
        LazyColumn(state = scrollState) {
            items(listSize) {
//                Text(text = "Item #$it", style = MaterialTheme.typography.titleLarge)
                ImageListItem(index=it)
            }
        }
    }
}

@OptIn(coil.annotation.ExperimentalCoilApi::class)
@Composable
fun ImageListItem(index: Int) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Image(
            painter = rememberImagePainter(
                data = "https://kimi-img.moonshot.cn/webimg2/img-blog.csdnimg.cn/5f4422e81144e74d540d69b44bc42fa5ad9bb62e?x-tos-process=image%2Fresize%2Cw_482"
            ),
            contentDescription = null,
            modifier = Modifier.size(50.dp)
        )

        Spacer(modifier = Modifier.size(10.dp))
        Text("text $index", style = MaterialTheme.typography.titleSmall)
    }
}